package com.xiaolanshu.controller;

import com.sun.tools.corba.se.idl.constExpr.Or;
import com.xiaolanshu.entity.Order;
import com.xiaolanshu.exception.BusinessException;
import com.xiaolanshu.interceptor.UserHolder;
import com.xiaolanshu.service.GoodsService;
import com.xiaolanshu.service.NoteService;
import com.xiaolanshu.service.OrderService;
import com.xiaolanshu.vo.ErrorResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private GoodsService goodsService;
    /**
     * 订单首页面
     * @return
     */
    @GetMapping
    public ResponseEntity orderIndex(){
        //从线程当中获取用户id和用户名（拦截器从请求头获取了用户信息并缓存到线程当中）
        Long userId = UserHolder.getUserId();
        //根据用户id查询订单列表
        List<Order> orderList = orderService.OrderListByUserzId(userId);

        return ResponseEntity.ok(orderList);
    }

    /**
     * 商品评论
     * @param comment
     * @param goodsId
     * @return
     */
    @PostMapping("/comment/{goodsId}")
    public ResponseEntity goodsComment(@RequestParam String comment,@PathVariable Long goodsId){
        //从线程当中获取用户id和用户名（拦截器从请求头获取了用户信息并缓存到线程当中）
        Long userId = UserHolder.getUserId();

        int i = goodsService.goodsComment(comment, goodsId, userId);

        if(!String.valueOf(i).equals("")){
            return ResponseEntity.status(200).body("评论成功");
        }
        else {
            //更新失败抛出自定义业务异常
            throw new BusinessException(ErrorResult.crudError());
        }

    }
}
